﻿
@{
    ViewBag.Title = "AddStaff";
    Layout = "~/Views/Shared/_Layout_iframe.cshtml";
}

<body>
    <form class="layui-form" action="" lay-filter="staffForm">
        <div class="layui-form-item">
            <label class="layui-form-label">员工工号</label>
            <div class="layui-input-inline">
                <input type="text" name="Numbers" lay-verify="required" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="Name" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input type="text" name="Phone" lay-verify="title" autocomplete="off" placeholder="请输入电话" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-block">
                <input type="text" name="CardId" lay-verify="title" autocomplete="off" placeholder="请输入身份证号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">银行卡号</label>
            <div class="layui-input-block">
                <input type="text" name="BankCard" lay-verify="title" autocomplete="off" placeholder="请输入银行卡号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select id="classes" name="RoleId" class="form-control">
                    @foreach (var role in ViewBag.role)
                    {
                        <option value="@role.Id">@role.RoleName</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-upload">
            <label class="layui-form-label">头像</label>
            <button type="button" class="layui-btn" id="test1">上传头像</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1">
                <p id="demoText"></p>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">入职时间</label>
            <div class="layui-input-inline layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="OnboardingTime" id="OnboardingTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="Pwd" lay-verify="title" autocomplete="off" placeholder="请输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="Sex" value="0" title="男" checked="">
                <input type="radio" name="Sex" value="1" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="Age" lay-verify="title" autocomplete="off" placeholder="请输入年龄" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        <input type="hidden" id="hfImgUrl" name="ImgUrl" />
        @if (ViewBag.staffid != null)
        {
            <input type="hidden" id="hfid" value="@ViewBag.staffid" />
        }
        else
        {
            <input type="hidden" id="hfNewStaffNumber" value="@ViewBag.NewStaffNumber" />
        }
    </form>
    <script src="~/Assets/bootstrap/js/jquery-1.10.2.min.js"></script>
    <script src="/Assets/PearAdmin/component/layui/layui.js"></script>
    <script src="/Assets/PearAdmin/component/pear/pear.js"></script>
    <script>
        var util,form;
        //绑定点击的员工的信息
        function initstaff() {
            let staffid = $("#hfid").val()
            $.post("/Staff/GetStaff/" + staffid, function (resp) {
                if (resp.code == 0) {
                    resp.data.OnboardingTime = util.toDateString(resp.data.OnboardingTime.replace('/Date(', '').replace(')/', ''), 'yyyy-MM-dd');
                    form.val('staffForm', resp.data)  //将各项数据绑定到相应的框中
                    $("#demo1").attr('src', resp.data.ImgUrl)
                }
            })
        }
        layui.use(['form', 'laydate', 'upload','util'], function () {
            form = layui.form;
            var laydate = layui.laydate;
            var upload = layui.upload;
            util = layui.util;
            //渲染日期
            laydate.render({
                elem: '#OnboardingTime'
            });
            // 仅在存在员工ID时执行初始化
            if ($("#hfid").length > 0 && $("#hfid").val()) {
                initstaff();
                //编辑时的form提交事件
                form.on('submit(demo1)', function (data) {
                    data.field.Id = $('#hfid').val();
                    $.post("/Staff/UpdateStaff", data.field, function (resp) {
                        if (resp.code == 0) {
                            //父页面的table重载
                            parent.layui.table.reload('testStaff')
                            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的系号
                            parent.layer.close(index);//再执行关闭
                        }
                    })
                    return false; // 阻止默认 form 跳转
                })
            } else {
                $('input[name="Numbers"]').val($("#hfNewStaffNumber").val());
                //添加时的form提交事件
                form.on('submit(demo1)', function (data) {
                    $.post("/Staff/AddStaff", data.field, function (resp) {
                        if (resp.code == 0) {
                            //父页面的table重载
                            parent.layui.table.reload('testStaff')
                            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的系号
                            parent.layer.close(index);//再执行关闭
                        }
                    })
                    return false; // 阻止默认 form 跳转
                })
            }
            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/Staff/UploadImg' 
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    else {
                        $("#hfImgUrl").val(res.url);
                    }
                }
            });
        })
    </script>
</body>

